package com.usabb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.usabb.domain.CombosCategoryDO;
import com.usabb.domain.CombosCategoryItemDO;
import com.usabb.pojo.vo.CombosCategoryAddVO;
import com.usabb.pojo.vo.CombosCategoryUpdateVO;
import com.usabb.service.CombosCategoryItemService;
import com.usabb.service.CombosCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <pre>
 * 月子中心 套餐 服务类目
 * </pre>
 * <small> 2019-08-19 18:03:27 | Jory</small>
 */
@Slf4j
@Controller
@RequestMapping("/usabb/maternityCombosCategory")
public class MaternityCombosCategoryController extends BaseController {
    @Autowired
    private CombosCategoryService combosCategoryService;
    @Autowired
    private CombosCategoryItemService combosCategoryItemService;

    @GetMapping()
    String MaternityCombos() {
        return "usabb/maternityCombosCategory/combosCategory";
    }

    @ResponseBody
    @GetMapping("/list")
    public Result<Page<CombosCategoryDO>> list(CombosCategoryDO categoryDO) {
        Long baseId = categoryDO.getBaseId();
        if (Objects.isNull(baseId)) {
            log.error("input baseId is null. so return.");
        }

        Wrapper<CombosCategoryDO> wrapper = new EntityWrapper<>(categoryDO).orderBy("id", false);
        Page<CombosCategoryDO> page = combosCategoryService.selectPage(getPage(CombosCategoryDO.class), wrapper);

        page.getRecords().forEach(it -> {
            final List<CombosCategoryItemDO> itemDOS = combosCategoryItemService.getByCategoryId(it.getId());
            it.setItems(itemDOS);
        });

        return Result.ok(page);
    }

    @GetMapping("/add/{baseId}")
    String add(@PathVariable("baseId") Long baseId, Model model) {
        model.addAttribute("baseId", baseId);
        return "usabb/maternityCombosCategory/add";
    }

    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Long id, Model model) {
        CombosCategoryDO categoryDO = combosCategoryService.getByIdWithItems(id);
        model.addAttribute("categoryDO", categoryDO);
        return "usabb/maternityCombosCategory/edit";
    }

    @Log("添加月子中心 套餐表")
    @ResponseBody
    @PostMapping("/save")
    public Result<String> save(CombosCategoryAddVO addVO) {
        log.info("Add input: {}", addVO);
        combosCategoryService.addCategories(addVO);
        return Result.ok();
    }

    @Log("修改月子中心 套餐表")
    @ResponseBody
    @RequestMapping("/update")
    public Result<String> update(CombosCategoryUpdateVO updateVO) {
        boolean update = combosCategoryService.updateCategory(updateVO);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除月子中心 套餐表")
    @PostMapping("/remove")
    @ResponseBody
    public Result<String> remove(Long id) {
        combosCategoryService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除月子中心 套餐表")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        combosCategoryService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
